package com.onenine.app.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Pair;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.onenine.app.R;
import com.onenine.app.activity.InviteReplyCreateActivity;
import com.onenine.app.adapter.InviteTabAdapter;
import com.onenine.app.model.MemberItem;
import com.onenine.app.request.AskInviteRequest;
import com.onenine.app.request.PageRequest;
import com.onenine.app.response.FriendAttentionResponse;
import com.onenine.app.utils.UrlConfig;
import com.onenine.base.base.BasePageListFragment;
import com.onenine.base.okhttp.BaseResponse;
import com.onenine.base.okhttp.OkCallback;
import com.onenine.base.okhttp.OkRequest;
import com.onenine.base.utils.AppConfig;
import com.onenine.base.utils.Toasty;
import com.onenine.base.view.PullRefreshRecyclerView;
import com.onenine.chat.rongcloud.RcUser;

/**
 * 邀请问答
 */
public class InviteTabFragment extends BasePageListFragment implements BaseQuickAdapter.OnItemChildClickListener {

    public static Fragment newInstance(Bundle args, int type) {
        InviteTabFragment fragment = new InviteTabFragment();
        args.putInt("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Pair<PullRefreshRecyclerView, RecyclerView.Adapter> initPullRefreshView(View view) {
        return Pair.create(view.findViewById(R.id.pull_to_refresh_layout)
                , new InviteTabAdapter());
    }

    @Override
    protected void initListener() {
        super.initListener();
        ((BaseQuickAdapter) mAdapter).setOnItemChildClickListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        loadPageData(1);
    }

    @Override
    protected void loadPageData(int pageNum) {
        PageRequest request = new PageRequest();
        request.setUserId(RcUser.getInstance().getUserId());
        request.setPageNum(pageNum);
        request.setPageSize(AppConfig.PAGE_SIZE);
        OkRequest.getInstance().withUrl(getArguments().getInt("type") == 0
                ? UrlConfig.GET_FRIEND_LIST : UrlConfig.GET_ATTENTION_LIST).withRequest(request)
                .doRequest(InviteTabFragment.class.getSimpleName(), new OkCallback<FriendAttentionResponse>() {
                    @Override
                    public void onResponse(FriendAttentionResponse response, String srcData) {
                        dismissRefreshStatus();
                        if (response.isSuccess()) {
                            notifyDataSuccess(response.getDataList());
                        } else {
                            notifyDataFailure();
                        }
                    }
                });
    }

    @Override
    protected int getLayout() {
        return R.layout.layout_pull_refresh_view;
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if (view.getId() == R.id.btn_invite) {
            submitData(((MemberItem) adapter.getItem(position)).getUserId());
        }
    }

    /**
     * 提交数据
     */
    private void submitData(String targetId) {
        AskInviteRequest request = new AskInviteRequest();
        request.setUserId(RcUser.getInstance().getUserId());
        request.setTargetUserIds(targetId);
        request.setCoteriePkId(getArguments().getString("postId"));
        request.setDescriptionContent(getArguments().getString("postContent"));
        OkRequest.getInstance().withUrl(UrlConfig.CREATE_INVITE_ASK)
                .withRequest(request).doRequest(InviteReplyCreateActivity.class.getSimpleName(), new OkCallback<BaseResponse>() {
            @Override
            public void onResponse(BaseResponse response, String srcData) {
                if (response.isSuccess()) {
                    Toasty.showNormal(R.string.invite_success);
                } else {
                    Toasty.showNormal(response.getMessage());
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        OkRequest.getInstance()
                .cancelTag(InviteTabFragment.class.getSimpleName());
    }

}
